<html>
<head><meta charset="utf-8"><title>Type inferrence &amp; irrefutable slice patterns · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html">Type inferrence &amp; irrefutable slice patterns</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209052937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209052937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209052937">(Sep 04 2020 at 04:00)</a>:</h4>
<p>I found out today that slice patterns in irrefutable contexts don't hint type inference:</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Iota</span><span class="p">.</span><span class="n">into</span><span class="p">();</span><span class="w"> </span><span class="c1">// ERROR: type annotations needed</span>
<span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">]</span>: <span class="p">[</span><span class="n">_</span><span class="p">;</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Iota</span><span class="p">.</span><span class="n">into</span><span class="p">();</span><span class="w"> </span><span class="c1">// Works great</span>
</code></pre></div>


<p>Play demo: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=6e4d6cc356df53399553d1dddf9896ef">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=6e4d6cc356df53399553d1dddf9896ef</a></p>
<p>I guess this is because in <em>refutable</em> position it can't hint array length.</p>
<p>Should this change?  Is it more confusing for <code>let [a, b, c] = </code> to not be understood to be a 3-array, or for <code>[a, b, c]</code> to have different inference consequences in different positions?</p>



<a name="209059864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209059864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209059864">(Sep 04 2020 at 06:41)</a>:</h4>
<p>we started to infer array length in <a href="https://github.com/rust-lang/rust/issues/70562">#70562</a> but only go from <code>[ty; _]</code> to <code>[ty; 3]</code> here</p>



<a name="209059880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209059880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209059880">(Sep 04 2020 at 06:41)</a>:</h4>
<p>So we only infer the length from a pattern if we already know that the type must be an array</p>



<a name="209059906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209059906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209059906">(Sep 04 2020 at 06:41)</a>:</h4>
<p>and as <code>Iota.into()</code> starts out as just <code>_</code> this means that we don't make progress here</p>



<a name="209060015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209060015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209060015">(Sep 04 2020 at 06:43)</a>:</h4>
<p>afaict we should be able to "just" directly infer the type in irrefutable patterns here, so that's something we may want to look into.</p>
<p>Feel free to open an issue for this so we don't forget</p>



<a name="209139465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Type%20inferrence%20%26%20irrefutable%20slice%20patterns/near/209139465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Type.20inferrence.20.26.20irrefutable.20slice.20patterns.html#209139465">(Sep 04 2020 at 20:50)</a>:</h4>
<p>Sounds good!  Filed <a href="https://github.com/rust-lang/rust/issues/76342">https://github.com/rust-lang/rust/issues/76342</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>